package com.iw.core.security.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iw.core.security.entity.Resources;
import com.iw.core.security.entity.ResourcesGroup;
import com.iw.core.security.entity.Users;
import com.iw.core.security.service.ResourcesService;
import com.iw.core.utils.RandomValidateCode;

@Controller
@RequestMapping("/frame")
public class PageFrameAction {

	@Resource
	private ResourcesService resourcesService;

	@RequestMapping(value = "/header")
	public String header(Model model, HttpServletRequest request) {
		return "/frame/header";
	}

	@RequestMapping(value = "/footer")
	public String footer(Model model) {
		return "/frame/footer";
	}

	@RequestMapping(value = "/accordion")
	public String accordion(Model model, HttpServletRequest request) {

		Users USER = (Users) request.getSession().getAttribute("USER");

		Map<Object, Object> groupMap = new LinkedHashMap<Object, Object>();
		Map<Object, Object> menuMap = new LinkedHashMap<Object, Object>();

		List<Resources> setResuorces = resourcesService.findMenu(USER.getId());
		for (Resources r : setResuorces) {
			// 是菜单项才添加
			if (r.isMenu()) {
				ResourcesGroup group = r.getGroup();
				// 是菜单项才添加
				if (group.isMenu()) {
					groupMap.put(group.getId().toString(), group);

					@SuppressWarnings("unchecked")
					List<Resources> list = (ArrayList<Resources>) menuMap.get(group.getId().toString());
					if (list == null) {
						list = new ArrayList<Resources>();
					}
					list.add(r);
					menuMap.put(group.getId().toString(), list);
				}
			}
		}

		model.addAttribute("groupMap", groupMap);
		model.addAttribute("menuMap", menuMap);

		return "/frame/accordion";
	}

	@RequestMapping(value = "/welcome")
	public String welcome(Model model, HttpServletRequest request) {
		return "/frame/welcome";
	}

	@RequestMapping(value = "/verifyCode")
	@ResponseBody
	public boolean verifyCode(HttpServletRequest request, HttpServletResponse response, String verifyCode) {
		HttpSession session = request.getSession();
		String code = String.valueOf(session.getAttribute(RandomValidateCode.RANDOMCODEKEY));
		if (verifyCode.equalsIgnoreCase(code)) {
			return true;
		} else {
			return false;
		}
	}

	@RequestMapping(value = "/validateCode")
	@ResponseBody
	public void validateCode(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
		response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expire", 0);
		RandomValidateCode randomValidateCode = new RandomValidateCode();
		try {
			randomValidateCode.getRandcode(request, response);// 输出图片方法
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/html")
	public String html(Model model) {
		model.addAttribute("message", "我的第一个FreeMarker程序");
		return "/01";
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
